<extend name="Public:main" />
<block name="title">注册</block>
<block name="custom_css">
    <link rel="stylesheet" href="__PUBLIC__/js/nice-validator-0.10.11/dist/jquery.validator.css">
</block>
<block name="body">
<body ng-app="myApp" ng-controller="Ctrl" ng-cloak>
    <div class="nav_top flex">
        <a  href="javascript:void(0)"  onclick="window.history.go(-1)">
            <i></i>
        </a>
        <div class="flex1">
            注册
        </div>
        <a href="javascript:void(0)">
            <i></i>
        </a>
    </div>
    <div style="height: 0.44rem;"></div>
    <form action="" method="post">
        <div class="login">
            <div>
                <label>邀请码</label>
                <input type="text" name="invitecode" value="{$invitecode}" placeholder="邀请码">
            </div>
            <div>
                <label>昵称</label>
                <input type="text" name="nickname" placeholder="昵称">
            </div>
        </div>
        <div class="login">
            <div>
                <label>手机号</label>
                <input type="text" name="account" placeholder="手机号" onInput ="returnNum(this)" maxlength="11">
            </div>
            <div>
                <label>验证码</label>
                <input type="text" name="code" placeholder="验证码" onInput ="returnNum(this)">
                <button class="get_yzm" id="getcode" onclick="getnum()">
                    获取验证码
                </button>
            </div>
        </div>
        <div class="login">
            <div>
                <label>登陆密码</label>
                <input type="password" name="password" placeholder="6-32位数字加字母" maxlength="32" minlength="6">
            </div>
            <div>
                <label>支付密码</label>
                <input type="password" name="paypassword" placeholder="6位数字支付密码" maxlength="6">
            </div>
        </div>
        <input class="login_btn" type="button" ng-click="reg()" value="注册">
        <div style="text-align: right;width: 88.8%;margin-top: 0.1rem;">
            <a style="color: #00a2d4;text-decoration: underline" href="/index.php/Home/Register/downapp?invitecode={$invitecode}">我已有账号，直接下载APP</a>
        </div>
    </form>
</body>
<block name="custom_js">
    <script src="__PUBLIC__/js/nice-validator-0.10.11/dist/jquery.validator.js"></script>
    <script>
        function getnum() {
            var time = 60;
            var t;
            var type=1; //reg

            $("#getcode").attr("disabled", true);
            $.post("{:U('Register/sendCode')}", {account: $("input[name='account']").val(), type: 1}, function (data) {
                if (data.code == 0) {
                    $("#getcode").html(time + "s");
                    t = setInterval(countdown, 1000);
                } else {
                    layer.msg(data.msg,{time:1000});
                    $("#getcode").attr("disabled", false);
                }
            });

            function countdown() {
                time = time - 1;
                $("#getcode").html(time + "s");
                $("#getcode").attr("disabled", true);
                if (time == 0) {
                    $("#getcode").html("获取验证码");
                    clearInterval(t);
                    $("#getcode").attr("disabled", false);
                }
            }
        }

        function returnNum(_this) {
            _this.value = _this.value.replace(/[^0-9]/g, '');
        }
    </script>
    <script>
        var app = getApp('myApp', []);
        app.controller('Ctrl', function ($scope, $http) {
            $scope.reg = function () {
                var valida = $('form').trigger("validate").data( "validator" ).isFormValid();//有表单验证的地方用 这个方法
                if (valida) {
                    $http({
                        method: 'POST',
                        data: {
                            "invitecode" : $("input[name='invitecode']").val(),
                            "nickname" : $("input[name='nickname']").val(),
                            "account" : $("input[name='account']").val(),
                            "code" : $("input[name='code']").val(),
                            "password" : $("input[name='password']").val(),
                            "paypassword" : $("input[name='paypassword']").val(),
                        },
                        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                        transformRequest: function (obj) {
                            var str = [];
                            for (var p in obj) {
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        },
                        url: "/index.php/Home/Register/validaform"
                    }).then(function successCallback(response) {
                        if(response.data.code==-1){
                            layer.msg(response.data.msg,{time:1000})
                        }
                        else if(response.data.code==0){
                            setTimeout(function () {
                                $scope.gotoreg();
                            },100)
                        }
                    })
                }
            }

            $scope.gotoreg = function () {
                $http({
                    method: 'POST',
                    data: {
                        "invitecode" : $("input[name='invitecode']").val(),
                        "nickname" : $("input[name='nickname']").val(),
                        "account" : $("input[name='account']").val(),
                        "code" : $("input[name='code']").val(),
                        "password" : $("input[name='password']").val(),
                        "paypassword" : $("input[name='paypassword']").val(),
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function (obj) {
                        var str = [];
                        for (var p in obj) {
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    },
                    url: "/index.php/Home/Register/register"
                }).then(function successCallback(response) {
                    if(response.data.code==-1){
                        layer.msg(response.data.msg,{time:1000})
                    }
                    else if(response.data.code==0){
                        layer.msg("注册成功",{time:1000})
                        setTimeout(function () {
                            window.location.href="/index.php/Home/Login/login"
                        },1500)
                    }
                }, function errorCallback(response) {
                    errorHandle(response);
                });
            }
        });
    </script>
</block>
</block>